//
//  VBDatabase.m
//  VicBattle
//
//  Created by Nguyen Nguyen on 5/7/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "VBDatabase.h"

@implementation VBDatabase

static VBDatabase *_data;

+(VBDatabase*)database {
    if (_data == nil) {
        _data = [[VBDatabase alloc] init];
    }
    return _data;
}

- (id)init {
    if ((self = [super init])) {
        NSString *sqLiteDb = [[NSBundle mainBundle] pathForResource:@"Database" 
                                                             ofType:@"sqlite"];
        
        if (sqlite3_open([sqLiteDb UTF8String], &_data) != SQLITE_OK) {
            NSLog(@"-----------------faile open database!");
        }
    }
    return self;
}

-(CGPoint) getCellByPos: (int) pos column:(int) col
{
    CGPoint new = CGPointZero;
    new.x = pos % col;
    new.y = pos / col;
    return new;
}

- (VBOffsetInfo *)initWithOffset:(NSString *)name offsetx:(int) x0 offsety:(int) y0{
    if (self = [super init]) {
        OffsetInfo = malloc(sizeof(OffsetInfo));
        OffsetInfo->name = name;
        OffsetInfo->x = x0;
        OffsetInfo->y = y0;
    }
    return OffsetInfo;
}

-(VBMapInfo *) initWithMap:(NSString *) name row:(int)row col:(int)col x0:(int)x0 y0:(int)y0
{
    if(self = [super init])
    {
        MapInfo = malloc(sizeof(MapInfo));
        MapInfo->name = name;  
        MapInfo->row = row;
        MapInfo->col = col;
        MapInfo->x0 = x0;
        MapInfo->y0 = y0; 
    }
    return MapInfo;
}

-(VBMapInfo *) getMapById:(int) idNumber
{
    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    {
        MapInfo=nil;
        NSString *query =[NSString stringWithFormat: @"SELECT namehd, row, col, x0hd, y0hd, data FROM Map where ID = %i",idNumber];
        sqlite3_stmt *statement;
        if (sqlite3_prepare_v2(_data, [query UTF8String], -1, &statement, nil) 
            == SQLITE_OK) {
            while (sqlite3_step(statement) == SQLITE_ROW) {
                char *name = (char *)sqlite3_column_text(statement, 0);
                int row = (int) sqlite3_column_int(statement, 1);
                int col = (int) sqlite3_column_int(statement, 2);
                float x0 = (float) sqlite3_column_double(statement, 3);
                float y0 = (float) sqlite3_column_double(statement, 4);
            
                char *datatemp = (char *)sqlite3_column_text(statement, 5);
                NSString *mapdata = [[NSString alloc] initWithUTF8String:datatemp];
                NSString *mapname = [[NSString alloc] initWithUTF8String:name];
                [self initWithMap:mapname row:row col:col x0:x0 y0:y0];
                
                for (int a = 0; a <= mapdata.length; a++) {
                    CGPoint newpoint = [self getCellByPos:a column:row];
                    MapInfo->data[(int) newpoint.x][(int)newpoint.y] = datatemp[a];
                }
                
                [mapname release];
                [mapdata release];
            }
            sqlite3_finalize(statement);
        }
        return MapInfo;
    }
    else
    {
        MapInfo = nil;
        NSString *query =[NSString stringWithFormat: @"SELECT name, row, col, x0, y0, data FROM Map where ID = %i",idNumber];
        sqlite3_stmt *statement;
        if (sqlite3_prepare_v2(_data, [query UTF8String], -1, &statement, nil) 
            == SQLITE_OK) {
            while (sqlite3_step(statement) == SQLITE_ROW) {
                char *name = (char *)sqlite3_column_text(statement, 0);
                int row = (int) sqlite3_column_int(statement, 1);
                int col = (int) sqlite3_column_int(statement, 2);
                float x0 = (float) sqlite3_column_double(statement, 3);
                float y0 = (float) sqlite3_column_double(statement, 4);
                
                char *datatemp = (char *)sqlite3_column_text(statement, 5);
                NSString *mapdata = [[NSString alloc] initWithUTF8String:datatemp];
                NSString *mapname = [[NSString alloc] initWithUTF8String:name];                
                [self initWithMap:mapname row:row col:col x0:x0 y0:y0];
                for (int a = 0; a <= mapdata.length; a++) {
                    CGPoint newpoint = [self getCellByPos:a column:row];
                    MapInfo->data[(int) newpoint.x][(int)newpoint.y] = datatemp[a];
                }
                
                [mapname release];
                [mapdata release];
            }
            sqlite3_finalize(statement);
        }
        return MapInfo;
    }
}

-(VBOffsetInfo *) getOffsetInfoById:(int) idNumber
{
    OffsetInfo = nil;
    NSString *query =[NSString stringWithFormat: @"SELECT elementname, offsetX, offsetY FROM OffSet where ID = %i",idNumber];
    sqlite3_stmt *statement;
    if (sqlite3_prepare_v2(_data, [query UTF8String], -1, &statement, nil) 
        == SQLITE_OK) {
        while (sqlite3_step(statement) == SQLITE_ROW) {
            char *name = (char *)sqlite3_column_text(statement, 0);
            float offsetx = (float) sqlite3_column_double(statement, 1);
            float offsety = (float) sqlite3_column_double(statement, 2);
            
            NSString *elename = [[NSString alloc] initWithUTF8String:name];
            
            if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
                [self initWithOffset:elename offsetx:offsetx offsety:offsety];
            else
                [self initWithOffset:elename offsetx:offsetx / 2 offsety:offsety / 2];
            
            [elename release];
        }
        sqlite3_finalize(statement);        
    }
    return OffsetInfo;
}

- (void)dealloc {
    sqlite3_close(_data);    
    [super dealloc];
}
@end
